//
//  JLProgressHUDManager.m
//  JLHousekeeper
//
//  Created by 吴嘉佳 on 2017/10/4.
//  Copyright © 2017年 吴嘉佳. All rights reserved.
//

#import "JLProgressHUDManager.h"
#import "MBProgressHUD.h"

static const float dalay = 1.5;

@interface JLProgressHUDManager()
@property (nonatomic ,strong) MBProgressHUD *hud;
@end
@implementation JLProgressHUDManager
+ (instancetype)shareInstance{
    static JLProgressHUDManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[JLProgressHUDManager alloc]init];
    });
    return instance;
}
/*********
 * 显示提示文字
 */
+ (void)showNoticeHud:(NSString *)notice{
    [JLProgressHUDManager showNoticeHud:notice targetView:nil hideBlock:nil];
}
+ (void)showNoticeHud:(NSString *)notice targetView:(UIView *)targetView{
    [JLProgressHUDManager showNoticeHud:notice targetView:targetView hideBlock:nil];
}

/*********
 * 显示成功文字
 */
+ (void)showSuccessHud:(NSString *)notice{
    [JLProgressHUDManager showNoticeHud:notice image: [UIImage imageNamed:@"success_indicator"] targetView:nil hideBlock:nil];
    


}
/*********
 * 显示失败文字
 */
+ (void)showErrorHud:(NSString *)notice{
    [JLProgressHUDManager showNoticeHud:notice image:[UIImage imageNamed:@"error_indicator"] targetView:nil hideBlock:nil];
    
}

+ (void)showNoticeHud:(NSString *)notice image:(UIImage *)image targetView:(UIView *)targetView hideBlock:(HideHudBlock)hideBlock{
    if (targetView == nil) {
        targetView = [JLProgressHUDManager frontView];
    }
    [MBProgressHUD hideHUDForView:targetView animated:YES];
    JLProgressHUDManager *hudManager = [JLProgressHUDManager shareInstance];
    hudManager.hud = [MBProgressHUD showHUDAddedTo:targetView animated:YES];
    if (image) {
        hudManager.hud.mode = MBProgressHUDModeCustomView;
        hudManager.hud.customView = [[UIImageView alloc] initWithImage:image];

    }else{
        hudManager.hud.mode = MBProgressHUDModeText;
    }
    
    hudManager.hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hudManager.hud.bezelView.color = [UIColor blackColor];
    hudManager.hud.label.font = [UIFont boldSystemFontOfSize:14.0];
    hudManager.hud.label.textColor = [UIColor whiteColor];
    hudManager.hud.label.text = notice;
    hudManager.hud.userInteractionEnabled = YES;

    // 隐藏
    [hudManager delayHideHud:dalay animated:YES hideBlock:hideBlock];
}


/*********
 * 加载框
 */

+ (void)showLoadingHud{
    [JLProgressHUDManager showLoadingHud:@"请稍候...." targetView:nil userInteractionEnabled:NO];
}
+ (void)showLoadingHud:(NSString *)notice{
    [JLProgressHUDManager showLoadingHud:notice targetView:nil userInteractionEnabled:NO];
}
+ (void)showLoadingHud:(NSString *)notice targetView:(UIView *)targetView{
    [JLProgressHUDManager showLoadingHud:notice targetView:targetView userInteractionEnabled:NO];
}
+ (void)showLoadingHud:(NSString *)notice targetView:(UIView *)targetView userInteractionEnabled:(BOOL)userInteractionEnabled{
    if (targetView == nil) {
        targetView = [JLProgressHUDManager frontView];
    }
    [MBProgressHUD hideHUDForView:targetView animated:YES];
    
    NSString *version = [UIDevice currentDevice].systemVersion;
    if (version.doubleValue >= 9.0) {
        [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[MBProgressHUD.class]].color = [UIColor whiteColor];
    }else{
        [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor whiteColor];
    }
    
    JLProgressHUDManager *hudManager = [JLProgressHUDManager shareInstance];
    hudManager.hud = [MBProgressHUD showHUDAddedTo:targetView animated:YES];
    hudManager.hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hudManager.hud.bezelView.color = [UIColor blackColor];
    hudManager.hud.label.font = [UIFont boldSystemFontOfSize:14.0];
    hudManager.hud.label.textColor = [UIColor whiteColor];
    hudManager.hud.label.text = notice;
    hudManager.hud.userInteractionEnabled = userInteractionEnabled;
}




// 获取当前最上方的屏幕
+ (UIView *)frontView{
    NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator];
    for (UIWindow *window in frontToBackWindows) {
        BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen;
        BOOL windowIsVisible = !window.hidden && window.alpha > 0;
        BOOL windowLevelSupported = (window.windowLevel >= UIWindowLevelNormal);
        BOOL windowKeyWindow = window.isKeyWindow;
        if(windowOnMainScreen && windowIsVisible && windowLevelSupported && windowKeyWindow) {
            return window;
        }
    }
//    UIWindow *window = frontToBackWindows.allObjects.lastObject;
    return [UIApplication sharedApplication].windows.lastObject;
}

// 隐藏提示框
+ (void)hideHud{
    [[JLProgressHUDManager shareInstance].hud hideAnimated:YES];
}


/**
 延时执行隐藏操作

 @param dalay 时间
 @param animated 动画
 @param hideBlock 回调
 */
- (void)delayHideHud:(float)dalay animated:(BOOL)animated hideBlock:(HideHudBlock)hideBlock{
    
    __weak typeof(self) weakSelf = self;
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(dalay * NSEC_PER_SEC));
    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
        [weakSelf.hud hideAnimated:animated];
        if (hideBlock) {
            hideBlock();
        }
    });
}
@end
